package cn.wolfcode.controller;

import cn.wolfcode.domain.Business;
import cn.wolfcode.domain.BusinessReport;
import cn.wolfcode.query.BusinessReportQueryObject;
import cn.wolfcode.service.IBusinessReportService;
import cn.wolfcode.service.IBusinessService;
import cn.wolfcode.statusenum.ConsumptionStatusEnum;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/Report")
public class BusinessReportController {

    @Autowired
    private IBusinessReportService businessReportService;
    @Autowired
    private IBusinessService businessService;

    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") BusinessReportQueryObject qo, Model model) {
        PageInfo<BusinessReport> pageInfo = businessReportService.list(qo);
        model.addAttribute("result", pageInfo);

        List<Business> businesses = businessService.selectAll();
        model.addAttribute("businesses", businesses);

        ConsumptionStatusEnum[] values = ConsumptionStatusEnum.values();
        model.addAttribute("consumptionStatuses", values);

        List<String> a1 = new ArrayList<>();
        List<Integer> a2 = new ArrayList<>();
        List<BigDecimal> a3 = new ArrayList<>();
        List<BigDecimal> a4 = new ArrayList<>();
        List<BigDecimal> a5 = new ArrayList<>();

        for (BusinessReport br:pageInfo.getList()){
            a1.add(br.getGroupmethod());
            a2.add(br.getCount());
            a3.add(br.getTotal());
            a4.add(br.getDiscount());
            a5.add(br.getPay());
        }

            String groupmethod_json = JSON.toJSONString(a1);
        String count_json = JSON.toJSONString(a2);
        String total_json = JSON.toJSONString(a3);
        String discount_json = JSON.toJSONString(a4);
        String pay_json = JSON.toJSONString(a5);


        model.addAttribute("groupmethod_json", groupmethod_json);
        model.addAttribute("count_json", count_json);
        model.addAttribute("total_json", total_json);
        model.addAttribute("discount_json", discount_json);
        model.addAttribute("pay_json", pay_json);

        return "businessReport/list";
    }
}
